package com.polarrose.wsf.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexpValidator implements Validator<String> {

	private Pattern pattern;

	public String validate(String o) {
		if (pattern == null) {
			return "Pattern cannot be null in regexp validator!";
		}
		Matcher matcher = pattern.matcher(o);
		if (matcher.matches()) {
			return null;
		} else {
			return "Parameter must match regular expression pattern " + pattern.pattern();
		}
	}

	public void setPattern(String pattern) {
		this.pattern = Pattern.compile(pattern);
	}
}
